/* @(#)GSMarketInfoDAO.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-5-26          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.dao;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import com.hitrust.gs.bean.GSMarketInfoBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSMarketInfoQryVO;

/**
 * @author JennyLi
 *
 */
public class GSMarketInfoDAO extends GSBaseDAO {
    
    /**
     * 查询市场信息
     * @param aVO           查询参数
     * @param pageNo        页号
     * @param pageSize      每页笔数
     * @return
     * @throws GSException
     */
    public ArrayList queryMarketInfoByOpt(GSMarketInfoQryVO aVO,int pageNo, int pageSize) throws  GSException{
        ArrayList tList = new ArrayList();
        StringBuffer tSql = new StringBuffer(this.getMarketInfoByOptSql(aVO));
        tSql.append(" order by GS_LASTMODIFY_DATE desc");
        try{
            tList = this.executeQuery(new GSMarketInfoQryVO(),tSql.toString(),null,pageNo,pageSize);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    /**
     * 查询市场信息     
     * @return
     * @throws GSException
     */
    public ArrayList queryMarketInfoAll() throws  GSException{
        ArrayList tList = new ArrayList();
        StringBuffer tSql = new StringBuffer("select A.*,B.GS_PROVIDER_SHORTNAME from GS_MARKET_INFO A,GS_PROVIDER B ");
        tSql.append(" where A.GS_PROVIDER_ID=B.GS_PROVIDER_ID ");        
        tSql.append(" order by GS_LASTMODIFY_DATE desc");
        try{
            tList = this.executeQuery(new GSMarketInfoQryVO(),tSql.toString(),null);            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    
    /**
     * 查询市场信息笔数 
     * @param aVO           查询参数
     * @return
     * @throws GSException
     */
    public int queryMarketInfoCountByOpt(GSMarketInfoQryVO aVO) throws GSException{
        int tSize = 0;
        StringBuffer tSql = new StringBuffer("select count(*) from (");
        tSql.append(getMarketInfoByOptSql(aVO));
        tSql.append(")");
        try{
            tSize = this.queryRowNum(tSql.toString(),null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tSize;
    }
    /**
     * 查询市场信息的SQL
     * @param aVO           查询参数
     * @return
     */
    private String getMarketInfoByOptSql(GSMarketInfoQryVO aVO){
        StringBuffer tSql = new StringBuffer("select A.*,B.GS_PROVIDER_SHORTNAME from GS_MARKET_INFO A,GS_PROVIDER B where");
        tSql.append(" A.GS_PROVIDER_ID=B.GS_PROVIDER_ID");
        tSql.append(" and to_char(to_date(A.GS_PUBLISH_DATE,'YYYY-MM-DD HH24:mi:ss'),'YYYYMMDD') < '" + aVO.publishDateEnd + "'");
        tSql.append(" and to_char(to_date(A.GS_PUBLISH_DATE,'YYYY-MM-DD HH24:mi:ss'),'YYYYMMDD') > '" + aVO.publishDateStr + "'");
        if(aVO.title != null && !aVO.title.trim().equals("")){
            tSql.append(" and A.GS_INFO_TITLE='" + aVO.title + "'");
        }
        if(aVO.content != null && !aVO.content.trim().equals("")){
            tSql.append(" and A.GS_INFO_CONTENT='" + aVO.content + "'");
        }
        if(aVO.providerId != null && !aVO.providerId.trim().equals("")){
            tSql.append(" and A.GS_PROVIDER_ID='" + aVO.providerId + "'");
        }
        GSLogWriter.info(this,tSql.toString());
        return tSql.toString();
    }
    /**
     * 新增市场信息
     * @param aMarketInfoBean       新增数据
     * @return
     * @throws GSException
     */
    public boolean insertMarketInfo(GSMarketInfoBean aMarketInfoBean) throws GSException{
        boolean tFlag = false; 
        StringBuffer tSql = new StringBuffer("insert into GS_MARKET_INFO (GS_INFO_TITLE,GS_INFO_CONTENT,GS_PUBLISH_DATE,GS_PROVIDER_ID,");
        tSql.append("GS_FILE_NAME,GS_CREATE_OPERATER,GS_CREATE_DATE,GS_LASTMODIFER_ID,GS_LASTMODIFY_DATE)values(");
        tSql.append("?,?,?,?,?,?,?,?,?)");
        
        Connection conn = null;
        try{
        	conn = this.getConnetion();
        	String tCurDT = DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
        	PreparedStatement pstmt = conn.prepareStatement(tSql.toString());
        	pstmt.setString(1,aMarketInfoBean.getGs_info_title());
        	String content = aMarketInfoBean.getGs_info_content();
        	pstmt.setCharacterStream(2,
                    new InputStreamReader(new ByteArrayInputStream(content.
                            getBytes("UTF-8")),"UTF-8"), content.length());
        	pstmt.setString(3,aMarketInfoBean.getGs_publish_date());
        	pstmt.setString(4,aMarketInfoBean.getGs_provider_id());
        	pstmt.setString(5,aMarketInfoBean.getGs_file_name());
        	pstmt.setString(6,aMarketInfoBean.getGs_create_operater());
        	pstmt.setString(7,tCurDT);
        	pstmt.setString(8,aMarketInfoBean.getGs_lastmodifer_id());
        	pstmt.setString(9,tCurDT);
        	
            pstmt.executeUpdate();
            tFlag = true;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }finally{
            close(conn);
        }
        return tFlag;
    }
    /**
     * 根据创建时间查询市场信息
     * @param aCreateDate       创建时间
     * @return
     * @throws GSException
     */
    public ArrayList queryMarketInfoByCD(String aCreateDate) throws GSException{
        ArrayList tList = new ArrayList();
        StringBuffer tSql = new StringBuffer("select A.*,B.GS_PROVIDER_SHORTNAME from GS_MARKET_INFO A,GS_PROVIDER B where");
        tSql.append(" A.GS_PROVIDER_ID=B.GS_PROVIDER_ID");
        tSql.append(" and A.GS_CREATE_DATE='" + aCreateDate + "'");
        try{
            tList = this.executeQuery(new GSMarketInfoQryVO(),tSql.toString(),null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    /**
     * 修改市场信息
     * @param aMarketInfoBean       修改数据
     * @return
     * @throws GSException
     */
    public boolean updateMarketInfo(GSMarketInfoBean aMarketInfoBean) throws GSException{
        boolean tFlag = false;
        StringBuffer tSql = new StringBuffer("update GS_MARKET_INFO set GS_INFO_CONTENT=?,GS_PUBLISH_DATE=?,");
        tSql.append("GS_PROVIDER_ID=?,GS_FILE_NAME=?,GS_LASTMODIFER_ID=?,GS_LASTMODIFY_DATE=? ");
        tSql.append("where GS_CREATE_DATE=? and GS_INFO_TITLE=?");
        
        Connection conn = null;
        try{
        	conn = this.getConnetion();
        	String tCurDT = DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
        	PreparedStatement pstmt = conn.prepareStatement(tSql.toString());

        	String content = aMarketInfoBean.getGs_info_content();
        	pstmt.setCharacterStream(1,
                    new InputStreamReader(new ByteArrayInputStream(content.
                            getBytes("UTF-8")),"UTF-8"), content.length());
        	pstmt.setString(2,aMarketInfoBean.getGs_publish_date());
        	pstmt.setString(3,aMarketInfoBean.getGs_provider_id());
        	pstmt.setString(4,aMarketInfoBean.getGs_file_name());
        	pstmt.setString(5,aMarketInfoBean.getGs_lastmodifer_id());
        	pstmt.setString(6,tCurDT);
        	pstmt.setString(7,aMarketInfoBean.getGs_create_date());
        	pstmt.setString(8,aMarketInfoBean.getGs_info_title());
        	
            pstmt.executeUpdate();
            tFlag = true;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }finally{
            close(conn);
        }
        return tFlag;    
    }
    
    /**
     * 修改附件
     * @param aMarketInfoBean
     * @return
     * @throws GSException
     */
    public boolean updateMarketInfoFile(GSMarketInfoBean aMarketInfoBean) throws GSException{
        boolean tFlag = false;
        StringBuffer tSql = new StringBuffer("update GS_MARKET_INFO set GS_FILE_NAME=?,GS_LASTMODIFER_ID=?");
        tSql.append(",GS_LASTMODIFY_DATE=? where GS_CREATE_DATE=? and GS_INFO_TITLE=?");
        Object[] tObj = new Object[5];
        tObj[0] = aMarketInfoBean.getGs_file_name();
        tObj[1] = aMarketInfoBean.getGs_lastmodifer_id();
        String tCurDT = DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
        tObj[2] = tCurDT;
        tObj[3] = aMarketInfoBean.getGs_create_date();
        tObj[4] = aMarketInfoBean.getGs_info_title();
        try{
            tFlag = this.executeUpdate(tSql.toString(),tObj); 
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;
    }    
    /**
     * 删除市场信息
     * @param aCreateDate           创建时间
     * @return
     * @throws GSException
     */
    public boolean deleteMarketInfo(String aCreateDate) throws GSException{
        boolean tFlag = false;
        String tSql = "delete from GS_MARKET_INFO where GS_CREATE_DATE='" + aCreateDate + "'";
        try{
            tFlag = this.executeUpdate(tSql,null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;
    }
}
